package com.xbongbong.paas.util;

import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;

import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author 吴峰
 * @date 2023/02/13 10:40
 */
public class ParentBusinessConditionUtil {

    /**
     * 产品条件封装
     * @param boolQueryBuilder
     * @param businessType
     * @param subBusinessType
     * @param attr
     */
    public static void productCondition(BoolQueryBuilder boolQueryBuilder, Integer businessType, Integer subBusinessType, String attr) {
        boolean isParent;
        // 库存查询也是sku了
        if (Objects.equals(subBusinessType, XbbRefTypeEnum.STOCK_SEARCH.getCode()) || Objects.equals(subBusinessType,XbbRefTypeEnum.WARNING_SEARCH.getCode()) || Objects.equals(subBusinessType,XbbRefTypeEnum.GUARANTEE_SEARCH.getCode()) || Objects.equals(subBusinessType,XbbRefTypeEnum.WAIT_PURCHASE.getCode()) || Objects.equals(subBusinessType,XbbRefTypeEnum.WAIT_PRODUCE.getCode())){
            isParent = false;
        }else {
            isParent = true;
        }
        if (Objects.equals(XbbRefTypeEnum.PRODUCT.getCode(),businessType)){
            if (Objects.equals(ProductEnum.BARCODE.getAttr(),attr)){
                boolQueryBuilder.mustNot(termQuery("data." + ProductEnum.PARENT_ID.getAttr(),0));
            }else {
                if (isParent){
                    boolQueryBuilder.filter(termQuery("data." + ProductEnum.PARENT_ID.getAttr(),0));
                }else {
                    boolQueryBuilder.mustNot(termQuery("data." + ProductEnum.PARENT_ID.getAttr(),0));
                }
            }
        }
    }
}
